package com.lianyu507.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;


public class DataUtil {

	// 字符串转日期对象
		public static java.sql.Date stringToDate(String dateString) {
			// yyyy-MM-dd
			Date date = null;
			SimpleDateFormat format = new SimpleDateFormat(Myconstant.yyyyMMdd);
			try {
				date = format.parse(dateString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return new java.sql.Date(date.getTime());
		}
		
		
		// 字符串转日期对象
			public static java.sql.Date stringToDateyy(String dateString) {
				// yyyy-MM-dd
				Date date = null;
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				try {
					date = format.parse(dateString);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return new java.sql.Date(date.getTime());
			}
			
		
		public static String dateToString(java.sql.Date date) {
			
			SimpleDateFormat format = new SimpleDateFormat(Myconstant.yyyyMMdd);
			String dateString = format.format(date);
		
			return dateString;
		}

		/**
		 * 用来计算发送两次验证码之间的间隔 () 
		 * @param d1
		 * @param d2
		 * @return
		 */
		public static long getBetweenSecond(Date d1 ,Date d2 ){
			return  Math.abs((d1.getTime() - d2.getTime())/1000) ;
		}
		/**
		 * 得到一天的最后一秒钟
		 * 
		 */
		public static Date endOfDay(Date d) {
			return DateUtils.addSeconds(
					DateUtils.addDays(DateUtils.truncate(d, Calendar.DATE), 1), -1);
		}

}
